﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSTOCK
{
    public partial class FMPOINT : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser,DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private int iGridView = 0;
        private string sType = "V";
        private string sSite_No = string.Empty;
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();
        private int iGridView2 = 0;
        private int detailindex = -1;
        DataTable myDetail;

        public FMPOINT()
        {
            InitializeComponent();
        }

        //数据保存成功后执行操作
        private void doProc()
        {
            string sql = " update TABLEVERSION set VERSION=VERSION+1 where TABLENAME='A_TS_POINT'";
            //myADO.doexec(sql);
            StaticClass.myDB.doexec(sql);

            myCache.doCache();
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = " select b.fpoint_name flab_name,c.flayer_name,a.* "
              +" from a_ts_point a,a_ts_point b,a_ts_layer c"
              +" where a.fpoint_flag = 0"
              +" and a.fparent_no = b.fpoint_no"
              +" and a.flayer_no=c.flayer_no";
            DataRowView dr1 = (DataRowView)cbfpoint_no.SelectedItem;
            if ((dr1 != null) && (cbfpoint_no.SelectedIndex > 0))
            {
                SQL = SQL + " and a.fparent_no='" + dr1.Row["fpoint_no"].ToString()+"'";
            }
            DataRowView dr2 = (DataRowView)cbflayer_no.SelectedItem;
            if ((dr2 != null) && (cbflayer_no.SelectedIndex > 0))
            {
                SQL = SQL + " and a.flayer_no='" + dr2.Row["flayer_no"].ToString() + "'";
            }
            if (qifpoint_no.Text != "")
            {
                SQL = SQL + " and ((a.fpoint_no like '%" + qifpoint_no.Text + "%')"
                 + " or (a.fpoint_name like '%" + qifpoint_no.Text + "%'))";
            }
            SQL = SQL + "  order by a.fparent_no,a.fpoint_no";
            //Qrytable = myADO.query(SQL);
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();
            rowindex = -1;
        }

        private void FMPOINT_Load(object sender, EventArgs e)
        {
            _INPUTTABLE = "import_i_ts_point";
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];
            sSite_No = strArray[0];

            //按钮控制
            sys07.Visible = false; sys07.Tag = "1";
            sys08.Visible = false; sys08.Tag = "1";
            sys09.Visible = false; sys09.Tag = "1";
            sys11.Text = "库存"; sys11.Visible = true;
            
            //下拉框
            DataTable myTs_Dict = myCache.doGetDict("A_TS_POINT", "fpoint_flag=2");
            uifparent_no.DataSource = myTs_Dict;
            uifparent_no.DisplayMember = "fpoint_name";
            uifparent_no.ValueMember = "fpoint_no";

            DataRow dr1 = myTs_Dict.NewRow();
            dr1["fpoint_name"] = "请选择";
            dr1["fpoint_no"] = "0";
            myTs_Dict.Rows.InsertAt(dr1, 0);
            cbfpoint_no.DataSource = myTs_Dict;
            cbfpoint_no.DisplayMember = "fpoint_name";
            cbfpoint_no.ValueMember = "fpoint_no";
            cbfpoint_no.SelectedIndex = 0;

            DataTable myTs_Layer = myCache.doGetDict("A_TS_LAYER", "");
            uiflayer_no.DataSource = myTs_Layer;
            uiflayer_no.DisplayMember = "flayer_name";
            uiflayer_no.ValueMember = "flayer_no";

            DataRow dr2 = myTs_Layer.NewRow();
            dr2["flayer_name"] = "请选择";
            dr2["flayer_no"] = "0";
            myTs_Layer.Rows.InsertAt(dr2, 0);
            cbflayer_no.DataSource = myTs_Layer;
            cbflayer_no.DisplayMember = "flayer_name";
            cbflayer_no.ValueMember = "flayer_no";
            cbflayer_no.SelectedIndex = 0;

            //执行查询代码
            OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }
        }

        //新增代码
        protected override void OnSys01()
        {
            uifpoint_no.Text = "";
            uifpoint_name.Text = "";
            uifperson.Text = "";
            uifvideo_url.Text = "";
            uifparent_no.SelectedValue = "";
            uiflayer_no.SelectedIndex = 0;
            
            sType = "A";
            doLoadData();
            uifpoint_no.ReadOnly = false;

            base.OnSys01();
        }

        //编辑+浏览载入数据
        private void doLoadData()
        {
            Boolean input = false;
            if ((sType.Equals("A")) || (sType.Equals("B")))
            {
                input = false;
            }
            else
            {
                input = true;
            }
            foreach (Control ctrl in uiPanelData.Controls)
            {
                if (ctrl is UITextBox)
                {
                    UITextBox m = (UITextBox)ctrl;
                    m.ReadOnly = input;
                }

                if (ctrl is UIComboBox)
                {
                    UIComboBox m = (UIComboBox)ctrl;
                    m.ReadOnly = input;
                }
            }

            if (!sType.Equals("A"))
            {
                uifpoint_no.Text = uiDGView["fpoint_no", rowindex].Value.ToString();
                uifpoint_name.Text = uiDGView["fpoint_name", rowindex].Value.ToString();
                uifvideo_url.Text = uiDGView["fvideo_url", rowindex].Value.ToString();
                uifperson.Text = uiDGView["fperson", rowindex].Value.ToString();

                int i = 0;
                string sfpoint_no = uiDGView["fparent_no", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uifparent_no.Items)
                {
                    string id = dr["fpoint_no"].ToString();
                    if (id.Equals(sfpoint_no))
                    {
                        uifparent_no.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }

                i = 0;
                string sflayer_no = uiDGView["flayer_no", rowindex].Value.ToString();
                foreach (System.Data.DataRowView dr in uiflayer_no.Items)
                {
                    string id = dr["flayer_no"].ToString();
                    if (id.Equals(sflayer_no))
                    {
                        uiflayer_no.SelectedIndex = i;
                        break;
                    }
                    i = i + 1;
                }
            }

            //载入明细
            string sql = "select a.fgoodno,b.fgoodname,b.fcasno,b.fspec,a.fsafeqty"
              +"  from i_ts_safeinv a,i_ts_good_info b"
              +"  where a.fpoint_no = '"+ uifpoint_no.Text+ "'"
              +"  and a.fgoodno = b.fgoodno"
              + " order by a.fgoodno";
            myDetail = StaticClass.myDB.query(sql);
            uiDetail.DataSource = myDetail;

            //表格列显示统一控制
            if (iGridView2 == 0)
            {
                myCache.doSetGridVIew(sMenu + "1", sUser, uiDetail);
                iGridView2 = iGridView2 + 1;
            }

            uibtn1.Visible = true;
            uibtn2.Visible = true;

            if (sType.Equals("V"))
            {
                uibtn1.Visible = false;
                uibtn2.Visible = false;
            }
            detailindex = -1;
        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "B";

            doLoadData();

            uifpoint_no.ReadOnly = true;

            base.OnSys02();
        }

        //删除代码
        protected override void OnSys03()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            if (this.ShowAskDialog("确定删除(" + uiDGView["fpoint_name", rowindex].Value.ToString() + ")?"))
            {
                string sBillNo = uiDGView["fpoint_no", rowindex].Value.ToString();
                string SQL = "exec proc_biz_dodelete 'a_ts_point','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);

                string sMsg = myTable.Rows[0][0].ToString();
                if (sMsg.Equals("OK"))
                {
                    this.ShowInfoDialog("删除完毕");
                    myCache.doCache();
                    OnSys04();
                }
                else
                {
                    this.ShowInfoDialog(sMsg);
                }
            }
        }

        //浏览代码
        protected override void OnSys10()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            sType = "V";

            doLoadData();

            base.OnSys10();

        }

        //数据为空检查
        private bool CheckData()
        {
            return CheckEmpty(uifpoint_no, "请输入实验室编号")
                   && CheckEmpty(uifpoint_name, "请输入实验室名称")
                   && CheckEmpty(uifparent_no, "请选择实验楼")
                   && CheckEmpty(uiflayer_no, "请选择楼层");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }
            DataRowView drv2 = (DataRowView)uifparent_no.SelectedItem;
            string sfpoint_no = drv2.Row["fpoint_no"].ToString();

            DataRowView drv1 = (DataRowView)uiflayer_no.SelectedItem;
            string sflayer_no = drv1.Row["flayer_no"].ToString();

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        a_ts_point myData = context.a_ts_point.Find(uifpoint_no.Text);
                        if (myData is null)
                        {
                            a_ts_point myData2 = new a_ts_point();
                            myData2.fpoint_no = uifpoint_no.Text;
                            myData2.fpoint_name = uifpoint_name.Text;
                            myData2.fparent_no = sfpoint_no;
                            myData2.fpoint_flag = 0;
                            myData2.fvideo_url = uifvideo_url.Text;
                            myData2.fperson = uifperson.Text;
                            myData2.foperator = sUser;
                            myData2.fupddate = DateTime.Now;
                            myData2.fsite_no = sSite_No;
                            myData2.fpoint_state = 1;
                            myData2.flayer_no = sflayer_no;
                            context.a_ts_point.Add(myData2);
                            context.SaveChanges();

                            doSetDetail();

                            doProc();
                            UIMessageTip.ShowOk("添加成功");
                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已存在");
                            //Console.WriteLine("已存在");
                            return;
                        }

                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                    UIMessageTip.ShowOk("新增失败:" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    a_ts_point myts_box = context.a_ts_point.Find(uifpoint_no.Text);

                    myts_box.fpoint_name = uifpoint_name.Text;
                    myts_box.fparent_no = sfpoint_no;
                    myts_box.fvideo_url = uifvideo_url.Text;
                    myts_box.fperson = uifperson.Text;
                    myts_box.foperator = sUser;
                    myts_box.fupddate = DateTime.Now;
                    myts_box.flayer_no = sflayer_no;
                    int result = context.SaveChanges();

                    doSetDetail();

                    doProc();
                    UIMessageTip.ShowOk("修改成功");
                }
            }

            //刷新cache
            sType = "V";
            base.OnSys08();

            OnSys04();
        }

        private void cbfpoint_no_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void cbflayer_no_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (iGridView > 0)
            {
                OnSys04();
            }
        }

        private void uibtn1_Click(object sender, EventArgs e)
        {
            //添加明细 a.fseq,a.fequipmentbarcode,b.fmacno,b.fequipmenttag fcatname
            if (uifpoint_no.Text=="")
            {
                UIMessageTip.ShowOk("实验室编号不能为空");
                return;
            }
            string sfpoint_no = uifpoint_no.Text;

            string sql = "select fgoodno,fcasno,fgoodname,fspec"
               +" from i_ts_good_info "
               +" where 1 = 1";

            //弹窗选择
            string DATA = doPOPWINDOS("物料查询"
                , "fgoodno,物料编号,121;fcasno,CAS号,121;fgoodname,物料名称名称,161"
                , sql
                , " order by fgoodno"
                , 1);
            Console.WriteLine("当前返回:" + DATA);

            if ((DATA is null) || (DATA.Length == 0))
            {
                return;
            }

            string[] strList = DATA.Split(';');

            for (int j = 0; j < strList.Length; j++)
            {
                string[] strArray = strList[j].Split(',');

                if (strArray.Length > 1)
                {
                    //存在不保存
                    DataRow[] foundRow = myDetail.Select("fgoodno = '" + strArray[0] + "'");
                    if (foundRow.Length == 0)
                    {
                        //不存在新增
                        DataRow dr = myDetail.NewRow();
                        dr["fgoodno"] = strArray[0];
                        dr["fcasno"] = strArray[1];
                        dr["fgoodname"] = strArray[2];
                        dr["fspec"] = strArray[3];
                        dr["fsafeqty"] = 0;
                        myDetail.Rows.InsertAt(dr, myDetail.Rows.Count);

                    }
                }
            }
        }

        private void uibtn2_Click(object sender, EventArgs e)
        {
            //删除明细
            Console.WriteLine("行:" + detailindex.ToString());
            if (detailindex == -1)
            {
                UIMessageTip.ShowOk("请先选择要删除的记录");
                return;
            }
            if (this.ShowAskDialog("确定删除吗?"))
            {
                string sfgoodno = myDetail.Rows[detailindex]["fgoodno"].ToString();
                DataRow[] foundRow = myDetail.Select("fgoodno = '" + sfgoodno+"'", "");
                foreach (DataRow row in foundRow)
                {
                    myDetail.Rows.Remove(row);
                }
            }
        }

        //保存明细
        private Boolean doSetDetail()
        {
            string sPoint_No = uifpoint_no.Text;
            bool IsOk = false;
            StaticClass.myDB.SQLList.Clear();
            string sql = "delete from i_ts_safeinv where fpoint_no='" + sPoint_No + "'";
            StaticClass.myDB.SQLList.Add(sql);

            for (int i = 0; i < myDetail.Rows.Count; i++)
            {
                sql = "insert into i_ts_safeinv(fpoint_no,fgoodno,fsafeqty)"
                    + " values('" + sPoint_No + "'"
                    + ",'" + myDetail.Rows[i]["fgoodno"].ToString() + "'"
                    + "," + myDetail.Rows[i]["fsafeqty"].ToString()
                    + ")";
                StaticClass.myDB.SQLList.Add(sql);
            }
            string sError = string.Empty;
            StaticClass.myDB.doSave(out IsOk, out sError);
            return IsOk;
        }

        //导入
        protected override void OnSys11()
        {
            doInputExecl("import_i_ts_safeinv");
        }

        private void uiDetail_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            detailindex = e.RowIndex;
        }

        private void uiDetail_DoubleClick(object sender, EventArgs e)
        {
            string iMenu = sMenu + "1";
            OnGridDbClick(iMenu, uiDetail);
        }
    }
}
